<script setup>
import { ref, watch } from 'vue';
import { getTextContents } from "@/api/post.js";

const props = defineProps({
  sectionId: Number // 接收 sectionId
});

const content = ref('');

// 监听 sectionId 的变化
watch(() => props.sectionId, async (newVal) => {
    if (newVal) {
      // 根据 sectionId 加载对应的文本内容
      getTextContents(newVal)
        .then((response) => {
          if (response.data.textContents && response.data.textContents.length > 0) {
            content.value = response.data.textContents[0].content
          } else {
            content.value = '数据库查无此项'; // 如果没有数据，设置为空
          }
        })
        .catch((error) => {
          console.error("Failed to fetch text content:", error)
        })
    }
  },
  { immediate: true } // 立即执行一次
)
</script>

<template>
  <div class="text-container">
    <h1 class="section-title">📖 内容展示</h1>
    <div class="content-box">
      <template v-if="content">
        <p class="content-text">{{ content }}</p>
      </template>
      <div v-else class="empty-state">
        <span class="loading-icon" v-if="!content">⌛</span>
        <p class="empty-text" v-else>⛔ 内容加载失败，请稍后重试</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.text-container {
  max-width: 800px;
  margin: 2rem auto;
  padding: 2rem;
  background: linear-gradient(145deg, #f8f9fa, #e9ecef);
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease;
}

.text-container:hover {
  transform: translateY(-2px);
}

.section-title {
  color: #2c3e50;
  font-size: 1.8rem;
  margin-bottom: 1.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid #3498db;
  display: inline-block;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.content-box {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  border: 1px solid #dfe6e9;
  min-height: 200px;
  position: relative;
}

.content-text {
  color: #34495e;
  font-size: 1.1rem;
  line-height: 1.8;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  flex-direction: column;
  gap: 1rem;
}

.loading-icon {
  font-size: 2rem;
  animation: spin 1s linear infinite;
}

.empty-text {
  color: #95a5a6;
  font-size: 1.2rem;
  margin: 0;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .text-container {
    margin: 1rem;
    padding: 1.5rem;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .content-text {
    font-size: 1rem;
  }
}
</style>
